
import { addressList,defaultAddress,createAddress,editAddress, queryAddress, delAddress } from "../apis/index.js";

import { User }  from "./user.js";
let user = new User()
class Address {
		
    addressList = []
	
	//代理模式
	constructor() {
		// 已经实例化直接返回实例对象
		if (typeof Address.instance === 'object') {
			return Address.instance;
		}

		// 缓存
		Address.instance = this;
		return this;

	}
		
	
	async getDefault() {
		return defaultAddress().then(defAddres => {
			return Promise.resolve(defAddres);
		})
	   
	}
	async getAll() {
		return addressList({
			pagenum: 1
		}).then(addressList => {
			this.addressList = addressList
			return Promise.resolve(addressList);
		})
	   
	}
	
	async queryAddress(addressId) {
		return queryAddress({
			addressId
		}) 
	}
	
	
	async createAddress(address) {
		return createAddress({
			...address,
			userName: user.username
		})
	   
	}
	
	async editAddress(address) {
		return editAddress({
			...address,
			userName: user.username
		})
	   
	}
	
	async delAddress(addressId) {
		return delAddress({
			addressId
		}) 
	}
	
}

export {
    Address
}